<?php

namespace app\common\service;


use app\common\model\FilePath;

class ServerAuth extends Base
{
    /**
     * @name: 模块列表
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-25 15:06:17
     */
    function moduleList ()
    {
        $return = model('ServerAuth')->field('id,product_name,module_name,product_key')->where('type',2)->select()->toArray();

        return $return;
    }

    function findAll($field = '*')
    {
        return db('server_auth')->field($field)->select();
    }


    /**
     * @name 授权列表
     * @param $params
     * @param $limit
     * @return array
     * @author wx
     * @date 2019/11/7 13:59
     */
    function list($params, $limit, $userGroupId)
    {

        $db = db('server_auth_module sam')
            ->join('server_user_group_module sugm', 'sam.id = sugm.module_id')
            ->where('group_id', $userGroupId);

        //条件筛选
        if(isset($params['product_name']) && !empty($params['product_name'])){
            $db->where('sam.module_name','like','%'.$params['product_name'].'%');
        }

        if(isset($params['stime']) && !empty($params['stime'])){
            $startTime = strtotime($params['stime']);
            $db->where('sam.stime','between',[$startTime, $startTime + 86400]);
        }
        if(isset($params['etime']) && !empty($params['etime'])){
            $endTime = strtotime($params['etime']);
            $db->where('sugm.etime','between',[$endTime, $endTime + 86400]);
        }

        $field = 'sugm.id, module_name as product_name, sugm.total as number, sam.stime, sugm.etime, sam.product_c_version';

        //查询查询数据
        $data = $db->order('id desc')->field($field)->paginate($limit)->toArray();

        return $data;
    }

    function findData($condition, $field = '*')
    {
        return db('server_auth')->field($field)->where($condition)->find();
    }

    /**
     * @name: 获取模块信息
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-03 15:51:12
     */
    function getModuleData ($moduleKey)
    {
        $return = $this->findData(['product_key' => $moduleKey], 'product_name,number');
        if(empty($return))
        {
            $this->setError($this->CODE_FAIL, '该授权不存在');
            return [];
        }

        return $return;
    }

    /**
     * @name: 授权剩余数量
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-03 16:01:43
     */
    function remainingNum($moduleKey)
    {
        $authData = $this->getModuleData($moduleKey);
        
        if (empty($authData))
        {
            return 0;
        }
    
        $HostModuleRelation = new HostModuleRelation();
        $existNum = $HostModuleRelation->existNum($moduleKey);
        
        $return = $authData['number'] - $existNum;
        $return > 0 ? : $return = 0;

        return $return;
    }

    /**
    * @name: 检测授权剩余数量
    * @Param: 
    * @Description: 
    * @Author: foo
    * @Date: 2019-12-04 15:59:22
    */
   function checkRemaining ($module)
   {
      $remianingNum = $this->remainingNum($module);//获取剩余授权数
      if ($remianingNum <= 0)
      {
         $this->setError($this->CODE_FAIL, '该模块可添加主机数量已达到上限');
         return false;
      }

      return true;
   }

    /**
     * @name 获取所有需要端口配置的模块
     * @author wx
     * @date 2019/12/14 14:33
     */
   function getAllPortMoudul()
   {
       return db('server_auth')->field('product_key')->where('is_port',1)->column('product_key');
   }

   function getModuleName($ProductKey)
   {
       return db('server_auth')->where('product_key',$ProductKey)->value('product_name');
   }
}